/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  VirtAcctStatusChangeService
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月10日
 *
 */
package cc.rengu.igas.acct.core.service.trans;

import cc.rengu.igas.acct.common.dao.AcctCustomerInfoMapper;
import cc.rengu.igas.acct.common.dao.AcctInfoMapper;
import cc.rengu.igas.acct.common.dao.MapperCommon;
import cc.rengu.igas.acct.common.dao.impl.AcctCustomerInfoMapperImpl;
import cc.rengu.igas.acct.common.dao.impl.AcctInfoMapperImpl;
import cc.rengu.igas.acct.common.entity.AcctInfo;
import cc.rengu.igas.acct.common.enums.AcctCustomerStatusEnum;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.igas.acct.common.enums.AcctStatusEnum;
import cc.rengu.igas.acct.common.util.CheckAcct3o1;
import cc.rengu.igas.acct.common.util.QueryAcct3o1;
import cc.rengu.igas.acct.facade.request.VirtAcctStatusChangeRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctStatusChangeResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;
import com.alibaba.fastjson.JSONObject;

/**
 * 账户状态修改
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/8
 */
public class VirtAcctStatusChangeService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            VirtAcctStatusChangeRequest virtAcctStatusChangeRequest = new VirtAcctStatusChangeRequest();
            ConvertUtil.convertOutput(virtAcctStatusChangeRequest);
            rglog.info("virtAcctStatusChangeRequest = <{}>", JSONObject.toJSONString(virtAcctStatusChangeRequest));
            VirtAcctStatusChangeResponse virtAcctStatusChangeResponse = new VirtAcctStatusChangeResponse();
            virtAcctStatusChangeResponse.setHeader(virtAcctStatusChangeRequest.getHeader());
            BizResponse<VirtAcctStatusChangeResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(virtAcctStatusChangeResponse);

            String dbPoolName = MapperCommon.getDbPoolName();
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);

            //查询,抛出异常无返回值
            AcctInfo acctInfo = QueryAcct3o1.queryAcctInfo(virtAcctStatusChangeRequest.getHeader().getInstId(),
                    virtAcctStatusChangeRequest.getAcctNo(), virtAcctStatusChangeRequest.getCustomerId(),
                    virtAcctStatusChangeRequest.getUserType(), virtAcctStatusChangeRequest.getUserId(), dbsUtil, rglog);

            //检查，抛出异常无返回值
            CheckAcct3o1.checkReqWithDb(virtAcctStatusChangeRequest.getAcctNo(),
                    virtAcctStatusChangeRequest.getCustomerId(), virtAcctStatusChangeRequest.getUserType(),
                    virtAcctStatusChangeRequest.getUserId(), acctInfo, rglog);

            //更新，抛出异常无返回值
            updAcctInfoStatus(virtAcctStatusChangeRequest.getAcctStatus(),
                    virtAcctStatusChangeRequest.getKeepCustomerInfo(), acctInfo, dbsUtil);

            bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
            virtAcctStatusChangeResponse.setAcctStatus(acctInfo.getAcctStatus());

            //修改状态为注销时返回客户信息状态为注销
            if (AcctStatusEnum.CANCEL.getAcctStatus().equals(virtAcctStatusChangeRequest.getAcctStatus())) {
                virtAcctStatusChangeResponse.setStatus(AcctCustomerStatusEnum.CANCEL.getStatus());
            }
            //修改为其他状态时返回客户信息状态为正常
            else {
                virtAcctStatusChangeResponse.setStatus(AcctCustomerStatusEnum.NORMAL.getStatus());
            }
            virtAcctStatusChangeResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            virtAcctStatusChangeResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(ChannelEnum.ACCT.getChannelType());
            bizResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
            ConvertUtil.convertInput(bizResponse.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    private void updAcctInfoStatus(String status, String keepCustomerInfo, AcctInfo acctInfo, DbsUtil dbsUtil) {
        AcctInfoMapper acctInfoMapper = new AcctInfoMapperImpl();
        AcctStatusEnum acctStatusEnum = AcctStatusEnum.getInstance(acctInfo.getAcctStatus());
        if (null == acctStatusEnum) {
            throw new BizException(AcctRespCodeEnum.ILLEGAL_OPR_ERROR.getRespCode(), AcctRespCodeEnum.ILLEGAL_OPR_ERROR.getRespDesc());
        }
        int retCode = acctStatusEnum.isOk(status);
        if (-1 == retCode) {
            rglog.error("当前状态<{}>不允许进行<{}>操作", acctStatusEnum.getAcctDesc(), acctStatusEnum.getAcctDesc());
            throw new BizException(AcctRespCodeEnum.ILLEGAL_OPR_ERROR.getRespCode(), AcctRespCodeEnum.ILLEGAL_OPR_ERROR.getRespDesc());
        } else if (0 == retCode) {
            rglog.debug("当前状态<{}>允许进行<{}>操作", acctStatusEnum.getAcctDesc(), acctStatusEnum.getAcctDesc());
            dbsUtil.dbsBeginTransaction();
            try {
                //更新账户信息表状态
                retCode = acctInfoMapper.updateAcctInfoStatusByCustomerId(acctInfo.getInstId(),
                        acctInfo.getCustomerId(), status, dbsUtil);
                if (Database.DBS_SUCCESS != retCode) {
                    if (Database.DBS_NOT_FOUND == retCode) {
                        throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                                AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                    } else {
                        throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(), AcctRespCodeEnum.DB_ERROR.getRespDesc());
                    }
                }
                //根据上送标志保留或删除客户信息
                cancelCustomerInfoByCustomerId(status, acctInfo.getInstId(), acctInfo.getCustomerId(), dbsUtil);
            } catch (BizException bizException) {
                dbsUtil.dbsEndTransaction(false);
                throw bizException;
            } catch (Exception e) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error(StringUtil.ExceptionToString(e));
                throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode(), OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
            }
            dbsUtil.dbsEndTransaction(true);
        } else {
            //该情况直接返回成功，避免调用方判断
            rglog.debug("当前状态<{}>与要进行操作<{}>一致", acctStatusEnum.getAcctDesc(), acctStatusEnum.getAcctDesc());
            throw new BizException(AcctRespCodeEnum.STATUS_NOT_ALLOW_ERROR.getRespCode(),
                    AcctRespCodeEnum.STATUS_NOT_ALLOW_ERROR.getRespDesc());
        }
    }

    private void cancelCustomerInfoByCustomerId(String status, String instId, String customerId, DbsUtil dbsUtil) {
        //判断当前操作是否为注销，若为注销则判断是否保留客户信息表示
        if (AcctStatusEnum.CANCEL.getAcctStatus().equals(status)) {
            AcctCustomerInfoMapper acctCustomerInfoMapper = new AcctCustomerInfoMapperImpl();
            //更新状态为注销
            if (Database.DBS_SUCCESS != acctCustomerInfoMapper.updCustomerStatusByCustomerId(instId, customerId, status, dbsUtil)) {
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(), AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        }
    }
}
